/*************************************************
 *FileName:      attention.js
 *Description:   我的关注.js
 *Others:        陈灿
 *************************************************/
    /*
     * 启动
     */
    mui.init();
    /*
     * 变量初始化
     */
    var uid = localStorage.replace_uid;
    var token = localStorage.replace_token;
    var ohtml = '';
    /*
     * 主程序
     */ 
    $(document).ready(function() 
    {
        //样式加载
          pagecss();
        //获取用户信息
    });
    /**
     * pagecss
     * 样式加载
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    function pagecss(){
        mui.plusReady(getlist)
        var onav ='';
        for(var i=0;i<26;i++){
            onav += '<a data-id="'+String.fromCharCode(97+i)+'">'+String.fromCharCode(65+i)+'</a>'
        }
        $('.nav .top').after(onav);//字母输出
        
        $('.main').on('tap','.userimg',function(){
            var oid = $(this).parents('.mf-table-view-cell').attr('data-id');
            common.pageTransition('personal.html?friend_uid='+oid)  
        })
        
        $('.serch-input').focusin(function(){
            $('.title').css({display:'none'});
            if(!$(this).val()){ $('.mf-table-view-cell').css({display:'none'})}
            $('nav.nav').css({display:'none'});
        }).on('input',function(){
            $('.mf-table-view-cell').css({display:'none'})
            if($(this).val()){
                for(var i =0 ; i<$('.mf-table-view-cell').length ;i++){
                    otext = $(this).val();
                    Cts = $('.mf-table-view-cell').eq(i).find('.user').text()+$('.mf-table-view-cell').eq(i).find('.phone').text();
                    Cts.indexOf(otext) > -1 ? $('.mf-table-view-cell').eq(i).css({display:'-webkit-flex'}) : false ;
                }
            }
        }).focusout(function(){
            if(!$(this).val()){
                $('.title').css({display:'block'});
                $('.mf-table-view-cell').css({display:'-webkit-flex'});
                $('nav.nav').css({display:'block'});
            }
        })//搜索效果
        
        $('.mui-scroll-wrapper').on('tap',function(){
            $('input').blur()
        })
        
        $('.nav a').on('tap', function() {
            var oheader = $('header.mui-bar').height() + $('.serch').height() + $('.title').height();
            var obj = '#' + $(this).attr('data-id');
            if(!$('.main').find($(obj)).length){return false;}
            var distance = -common.getTop($(obj)[0]) //计算高度
            var osum = distance + oheader;
            mui('.mui-scroll-wrapper').scroll().scrollTo(0, osum, 100);
        }) //字母跳转
        
        $('.nav .top').on('tap',function(){
            mui('.mui-scroll-wrapper').scroll().scrollTo(0,0,100);//100毫秒滚动到顶
        })//置顶
        $('.nav .bottom').on('tap',function(){
            mui('.mui-scroll-wrapper').scroll().scrollToBottom(0,0,100);//100毫秒滚动到顶
        })//置低
        
        
        $('.main').on('tap','.abolish',function(){
            var obj =$(this);
            var oname = $(this).parents('.mf-table-view-cell').find('.user').text();
            mui.confirm('您确定取消对'+oname+'的关注？','提示',['确定','取消'],function(e){
                if(e.index==0){
                    abolish(obj);
                }
            },'div')
           
        })
    }


//获取用户信息
function getlist() {
    $.ajax({
        type: "post",
        url: DOMAIN_NAME + "/index.php/api/users_attention/myattentionList",
        dataType: 'jsonp',
        data: {
            uid: uid,
            token: token
        },
        success: function(data) {
            if(data.error_code == 0) {
                for(var i = 0; i < data.data.length; i++) {
                    var su,sn = data.data[i].text;
                    data.data[i].text == '' ?su = '#':su = data.data[i].text;
                    ohtml += '<a class="title" id="' + sn.toLowerCase() + '">' + su + '</a>';
                    for(var j = 0; j < data.data[i].data.length; j++) {
                        ohtml += '<a class="mf-table-view-cell" data-id="' + data.data[i].data[j].friend_uid + '"><div class="userimg">';
                        data.data[i].data[j].friend_new ? ohtml += '<em class="new"></em>' : ohtml += '';
                        ohtml += '<img src="' +DOMAIN_NAME+ data.data[i].data[j].face + '" onerror="imgExists(this)" />' +
                            '</div><div class="mf-navigate-right">' +
                            '<div class="user">' + data.data[i].data[j].nickname + '</div><div class="age">';
                        data.data[i].data[j].sex == 'woman' ? ohtml += '<img src="images/icon_girl.png"/>' : ohtml += '<img src="images/icon_boy.png"/>';
                        ohtml += '<span>' + data.data[i].data[j].age + '</span></div>';
                        data.data[i].data[j].level ? ohtml += '<div class="age sex"><span>' + data.data[i].data[j].level + '级魅力</span></div></div><button class="abolish">取消关注</button></a>' : ohtml += '</div><button class="abolish">取消关注</button></a>';
                    }
                }
                $('.main').html(ohtml);
            } else if(data.error_code == 1001) {
                setTimeout(function() {
                    exitlogin();
                },1000);
            } else {

            }
            mui('.mui-scroll-wrapper').scroll({
                indicators: false, //是否显示滚动条 默认为true
                deceleration: 0.003, //阻尼系数,系数越小滑动越灵敏 默认0.0006
                bounce: true //是否启用回弹 默认true
            });
        }
    });
}


//取消关注
function abolish(obj) {
    var friend_uid =obj.parents('.mf-table-view-cell').attr('data-id');
    var othat=obj.parents('.mf-table-view-cell');
    $.ajax({
        type: "post",
        url: DOMAIN_NAME + "/index.php/api/users_attention/attentionCancel",
        dataType: 'jsonp',
        data: {
            uid: uid,
            token: token,
            friend_uid,friend_uid,
        },
        success: function(data) {
            if(data.error_code == 0) {
                if(othat.prev().hasClass('title')&&( othat.next().hasClass('title')||othat.next().length==0 )){
                    othat.prev('.title').remove();
                }
                othat.remove();
            } else if(data.error_code == 1001) {
                setTimeout(function() {
                    exitlogin();
                },1000);
            } else {
            }
        }
    });
}